package ru.fadeev.crammer.ui.window;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import ru.fadeev.crammer.Settings;
import ru.fadeev.crammer.ui.LimitedPlainDocument;

/**
 * SpecifyBatchNameDialog class provides GUI for entering batch name
 * when adding phrases to phrasesCorpus in SettingsDialog.
 * @author Konstantin Fadeyev
 *
 */
public class SpecifyBatchNameDialog extends JDialog {
	// Constants
	public final static int BTN_OK = 100;
	public final static int BTN_CANCEL = 101;
	
	// UI Components
	private JPanel mainPanel;
	private JLabel batchNameLabel;
	private JTextField batchNameTextField;	
    private JButton okButton;
    private JButton cancelButton;
    
	// User fields
	private int pressedButton;
    
	
	/**
	 * Constructs SpecifyBatchNameDialog and initializes it with name.
	 * @param owner parent window
	 * @param initName initial name
	 */
    public SpecifyBatchNameDialog(Dialog owner, String initName) {
        super(owner,true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(410, 100);
        setLocation((screenSize.width-410)/2, (screenSize.height-100)/3);
        
        setResizable(false);
        setTitle("Specify batch name");
        pressedButton = BTN_CANCEL;
        
        initComponents();
        placeComponents();
        
        batchNameTextField.setText(initName);
    }
    
    /**
     * Initializes UI Components of dialog
     */
    private void initComponents() {
        mainPanel = new JPanel();
        batchNameLabel = new JLabel("Batch name:");  
        batchNameTextField = new JTextField();
    	LimitedPlainDocument batchNameDocument = 
			new LimitedPlainDocument(Settings.BATCH_NAME_MAX_LENGTH);
    	batchNameTextField.setDocument(batchNameDocument);
        okButton = new JButton("OK");
        getRootPane().setDefaultButton(okButton);
        
		cancelButton = new JButton("Cancel");
        okButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	okButtonAction();
			        }
			    });
        
        cancelButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	cancelButtonAction();
			        }
		        });
        
        setContentPane(mainPanel);
    }

    
    /**
     * Places UI components using different layout managers
     */
    private void placeComponents() {
    	mainPanel.setLayout(null);
    	mainPanel.add(batchNameLabel);
    	mainPanel.add(batchNameTextField);
    	mainPanel.add(okButton);
    	mainPanel.add(cancelButton);  
    	  	
    	batchNameLabel.setBounds(10, 10, 60, 20);
    	batchNameTextField.setBounds(80, 10, 310, 20);
    		
    	okButton.setBounds(200, 40, 90, 20);
    	cancelButton.setBounds(300, 40, 90, 20);
    }
    
    
    /**
     * Ok button action handler. Closes dialog.
     */
    private void okButtonAction() {    	
    	pressedButton = BTN_OK;
    	setVisible(false);
    }
    
    
    /**
     * Cancel button action hadler. Closes dialog.
     */
    private void cancelButtonAction() {
        pressedButton = BTN_CANCEL;
        setVisible(false);
    }
    
    
    /**
     * After dialog is closed, call this function to find out which option
     * user picked (OK or Cancel)
     * @return pressed button (one of BTN_OK, BTN_CANCEL)
     */
    public int getPressedButton() {
    	return pressedButton;
    }
    
    
    /**
     * After dialog is closed, call this function to find out what batch name
     * entered user
     * @return batch name
     */
    public String getBatchName() {
    	return batchNameTextField.getText();
    }
}
